package gui.Stu;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import java.util.Calendar;
import info.*;
import sql.Connect;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import tool.checkDate;
public class Subscribe {
    SubscribeForm SubForm = new SubscribeForm();
    private  User stu ;
    private checkDate cd = new checkDate();

    public JPanel createSubscribe(CardLayout cardLayout,JPanel mainPanel,Connect conn, User user){
        stu = user;
        JPanel subscribePage = new JPanel();
        subscribePage.setLayout(new BoxLayout(subscribePage, BoxLayout.Y_AXIS));
        JPanel topBox = new JPanel();
        JLabel WelText = new JLabel("填写请假申请");
        JButton  historyBtn = new JButton("查看历史");

        //跳转history按钮
        historyBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                cardLayout.show(mainPanel, "History");
            }
        });
        topBox.add(WelText);
        topBox.add(historyBtn);



        // 创建开始日期时间选择器

        JPanel StartBox = new JPanel();
        SpinnerDateModel startModel = new SpinnerDateModel(new Date(), null, null, java.util.Calendar.MINUTE);
        JSpinner startSpinner = new JSpinner(startModel);
        JSpinner.DateEditor startEditor = new JSpinner.DateEditor(startSpinner, "yyyy-MM-dd HH:mm:ss");
        startSpinner.setEditor(startEditor);

        // 创建结束日期时间选择器
        JPanel EndBox = new JPanel();
        SpinnerDateModel endModel = new SpinnerDateModel(new Date(), null, null, java.util.Calendar.MINUTE);
        JSpinner endSpinner = new JSpinner(endModel);
        JSpinner.DateEditor endEditor = new JSpinner.DateEditor(endSpinner, " yyyy-MM-dd HH:mm:ss  ");
        endSpinner.setEditor(endEditor);

        //请假类型
        JPanel typeBox = new JPanel();
        JLabel typeText  = new JLabel("请假类型：");
        JComboBox SubType = new JComboBox();
        SubType.addItem("事假");
        SubType.addItem("病假");
        SubType.addItem("公假");
        SubType.setSize(10,20);
        typeBox.add(typeText);
        typeBox.add(SubType);
        SubType.setSelectedIndex(0);
        // 添加 ActionListener
        SubType.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 获取所选项的文本
                String selectedItem = (String) SubType.getSelectedItem();
                System.out.println("已选择的请假类型: " + selectedItem);
                SubForm.setType(selectedItem);
                // 你可以在这里进行其他操作，例如更新界面或保存数据
            }
        });

        //请假理由
        JPanel ReasonBox = new JPanel();
        JTextArea ReasonInput = new JTextArea("请填写请假的具体事由", 20, 50);
        ReasonBox.add(ReasonInput);

        ReasonInput.addFocusListener(new FocusAdapter() {
            @Override
            public void focusGained(FocusEvent e) {
                // 清空默认文本
                if (ReasonInput.getText().equals("请填写请假的具体事由")) {
                    ReasonInput.setText("");
                }
            }
        });

        //添加请假理由监听
        ReasonInput.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void insertUpdate(DocumentEvent e) {
                updateReason();
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                updateReason();
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                updateReason();
            }
            private void updateReason(){
                String text = ReasonInput.getText();
                SubForm.setReason(text);
                System.out.println("请假理由："+SubForm.getReason());
            }
        });
        //添加时间监听
        startSpinner.addChangeListener(e -> {
            Date startDate = (Date) startSpinner.getValue();
            //检查新输入的开始时间是否早于结束时间
            if( cd.checkStart(startDate,SubForm.getEndDate())){
                System.out.println("Start date changed: " + SubForm.getStartDate());
                SubForm.setStartDate(startDate);
            }else{
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(SubForm.getEndDate());
                calendar.add(calendar.YEAR, -1);
                startSpinner.setValue(calendar.getTime());
                JOptionPane.showMessageDialog(null, "开始时间不得晚于结束时间", "错误", JOptionPane.ERROR_MESSAGE);
            }

        });

        endSpinner.addChangeListener(e -> {
            Date endDate = (Date) endSpinner.getValue();
            //检查新输入的结束时间是否晚于开始时间
            if (cd.checkEnd(endDate,SubForm.getStartDate())){
                SubForm.setEndDate(endDate);
                System.out.println("End date changed: " + SubForm.getEndDate());
            }else{
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(SubForm.getStartDate());
                calendar.add(calendar.YEAR, 1);
                endSpinner.setValue(calendar.getTime());
                JOptionPane.showMessageDialog(null, "结束时间不得早于开始时间", "错误", JOptionPane.ERROR_MESSAGE);

            }

        });
        // 添加组件到面板

        StartBox.add(new JLabel("开始时间:"));
        StartBox.add(startSpinner);

        EndBox.add(new JLabel("结束时间:"));
        EndBox.add(endSpinner);


        //提交按钮
        JButton submitBtn = new JButton("提交");
        submitBtn.setPreferredSize(new Dimension(100,50));
        //添加提交监听
        submitBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("请假理由:"+SubForm.getReason());
                System.out.println("请假开始时间:"+SubForm.getStartDate());
                System.out.println("请假结束时间:"+SubForm.getEndDate());
                System.out.println("user.getNumber():"+stu.getNumber());
                System.out.println("user.getNumber():"+stu.getName());
                SubForm.setNumber(stu.getNumber());
                SubForm.setName(stu.getName());
                int result = conn.addSubscribe(SubForm);
                if(result>0){
                    JOptionPane.showMessageDialog(null,"提交请假成功,请联系辅导员审批","成功",JOptionPane.INFORMATION_MESSAGE);
                }
            }
        });

        subscribePage.add(topBox);
        subscribePage.add(StartBox);
        subscribePage.add(EndBox);
        subscribePage.add(typeBox);
        subscribePage.add(ReasonBox);
        subscribePage.add(submitBtn);
        return subscribePage;
    }

}
